iT邦幫忙

2022 iThome 鐵人賽

DAY 16
0
自我挑戰組

ASP.NET & SQL Server系列 第 16

自己動手撰寫資料庫連結程式(4)

  • 分享至 

  • xImage
  •  

嗨嗨!今天就來讓我們來看一下這幾天寫的程式碼帶來的成果是甚麼吧!

https://ithelp.ithome.com.tw/upload/images/20220926/20152450Bkp9ZMO1Wy.png

讀者們可以看到這跟筆者先前教大家使用簡易的版本撰寫出來的程式的結果一樣!!

那今天筆者要再各位看幾個不一樣的範例!!

那這次就先一次全部將程式碼貼出來,提供給各位參考以及比較

這個是刪除資料的程式碼(DELETE)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data.SqlClient;

namespace DeleteSql
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {   int a = 69;
            SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString);
            conn.Open();

            string sqlstr = "DELETE from table1 where [id] = @id";

            SqlCommand cmd = new SqlCommand(sqlstr, conn);

            cmd.Parameters.AddWithValue("id", a);

            cmd.ExecuteNonQuery();

            cmd.Cancel();
            conn.Close();


            Response.Write("have been delete");
        }
    }
}

這個是INSERT一筆資料的寫法


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data.SqlClient;

namespace WebApplication22
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {   
            
            
            SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString)
            {
                int a = 69;

                string b = "cxz";

               int c = 890;

     
              string sqlstr = "insert into table1 ([id], [name], [value]) values(@id, @name, @value)";



                SqlCommand cmd = new SqlCommand(sqlstr, conn);
                
                conn.Open();

                cmd.Parameters.AddWithValue("id", a);
                cmd.Parameters.AddWithValue("name", b);
                cmd.Parameters.AddWithValue("value", c);

                cmd.ExecuteNonQuery(); 
                

             
                cmd.Cancel();
             

            
            }

           

        }
    }
}

這個是UPDATE一筆資料的寫法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data.SqlClient;

namespace UpdateSql
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int a = 69;
            string b = "newbrookie";
            int c = 10;
            SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString);
                           
            
            string sqlstr  = "UPDATE table1 SET [name] = @name ,[value]= @value WHERE [id] = @id";
            
            SqlCommand cmd = new SqlCommand(sqlstr, conn);
            
            conn.Open();
            cmd.Parameters.AddWithValue("id", a);
            cmd.Parameters.AddWithValue("name", b);
            cmd.Parameters.AddWithValue("value", c);

            cmd.ExecuteNonQuery();
            cmd.Cancel();
            conn.Close();

        }
    }
}


上一篇
自己動手撰寫資料庫連結程式(3)
下一篇
自己動手撰寫資料庫連結程式(Delete)
系列文
ASP.NET & SQL Server30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言